package com.xyh.concurrency.example.Thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * 演示callable 异步的调用线程返回结果
 */
@Slf4j
public class FutureExample {
    static class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            log.info("线程返回");
            Thread.sleep(5000);
            return "hello";
        }
    }

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> submit = executorService.submit(new MyCallable());
        //继续向下执行等待结果输出
        log.info("输出结果");
        //线程会一直等到,当前的地址执行完成
        executorService.shutdown();
        System.out.println("输出结果" + submit.get());

    }
}
